package com.athui.utils.explorer.strategy;

import com.athui.bean.metadata.TableMetaData;

import java.util.stream.Collectors;

/**
 * @description: TODO 统计策略
 * @autor: zhangzhonghui
 * @create: 2024-08-02 12:16
 * @Version: 1.0
 */
public abstract class ExplorerStrategy {
    // 全表扫描
    public static final String FULL_SCAN_STRATEGY = "FULL_SCAN";
    // 数据抽样
    public static final String SAMPLING_STRATEGY = "SAMPLING";


    private TableMetaData tableMetaData;



    /**
     * @description: TODO 采样统计
     * @autor: zhangzhonghui
     * @param: columnMetaDataList
     * @param: limit    条数限制
     * @return: java.lang.String 执行的sql
     * @create: 2024/8/2 12:31
     * @Version: 1.0
    **/
    public abstract String sampling(Integer limit);


    /**
     * @description: TODO 全表扫描，限制条数
     * @autor: zhangzhonghui
     * @param: limit 条数限制
     * @return: java.lang.String
     * @create: 2024/8/2 12:40
     * @Version: 1.0
    **/
    public abstract String fullScan(Integer limit);


    /**
     * @description: TODO
     * @author: 阿辉大人
     * @param: limit
     * @param: strategy
     * @return: java.lang.String
     * @create: 2024/8/3 15:25
     * @version: 1.0
    **/
    public String build(Integer limit,String strategy){
        String sql = "";
        if(FULL_SCAN_STRATEGY.equals(strategy)){
            sql = fullScan(limit);
        }else if(SAMPLING_STRATEGY.equals(strategy)){
            sql = sampling(limit);
        }
        System.out.println(sql);
        return sql;
    }


    public String getFields(){
        return this.tableMetaData.getColumnMetaDataList().stream()
                .map(e-> e.getColumnName() )
                .collect(Collectors.joining(", "));

    }

    public String getFullTable(){
        return String.format("%s.%s", this.tableMetaData.getSchemaName(),this.tableMetaData.getTableName());
    }


    public ExplorerStrategy setTableMetaData(TableMetaData tableMetaData) {
        this.tableMetaData = tableMetaData;
        return this;
    }


}
